package com.seeyon.apps.ext.cop.button.service.impl;

import com.seeyon.apps.ext.cop.button.service.DataBtnService;
import com.seeyon.apps.snail.DIPPluginInitialization;
import snail.common.utils.MapHelper;
import snail.common.utils.cipher.AESCoder;
import snail.common.utils.db.DBHelper;
import snail.common.utils.db.DataSourceHelper;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataBtnServiceImpl implements DataBtnService {

    private static String url = "jdbc:mysql://10.211.55.2:3306/xmx?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8";// 连接URL字符串
    private static String user = "root";// 数据库用户名
    private static String password = "Mysql123456";// 用户密码


    public static Object DataServer(long formId, long btnId, List<Map<String, Object>> dataList) {

        try {

            DataSource ds = DataSourceHelper.getMySQL6XDatasource(url, user, password);
            DBHelper dbhelper = DBHelper.getInstance(ds);

            for (Map<String, Object> tool : dataList) {

                String sql = "insert into inventory(id, cInvCode, cInvName, cInvStd, cInvAddCode) value (:id, " +
                        ":cInvCode, :cInvName, :cInvStd, :cInvAddCode)";
                HashMap<String, Object> hashTool = new  HashMap<>();
                hashTool.put("id", tool.get("id"));
                hashTool.put("cInvCode", MapHelper.getString(tool, "field0001.value"));
                hashTool.put("cInvName", MapHelper.getString(tool, "field0002.value"));
                hashTool.put("cInvStd", MapHelper.getString(tool, "field0003.value"));
                hashTool.put("cInvAddCode", MapHelper.getString(tool, "field0004.value"));
                int queryForList2 = dbhelper.update(sql, hashTool);
                if (queryForList2 > 0) {
                    return "新增成功";
                }
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        Long id = 1232131L;
        Long ids = 32234324L;
        List<Map<String, Object>> a = new ArrayList();
        Map map = new HashMap();
        map.put("field0001", "code");
        map.put("field0002", "dd");
        map.put("field0003", "cc");
        map.put("field0004", "bb");
        map.put("id", "135");
        a.add(0, map);
        System.out.println(DataServer(id, ids, a));
    }
}
